//
// Created by lfm on 25-4-14.
//

#include <iostream>
#include <algorithm>
using namespace std;
template <class T>
class MyArray
{
public:
    //有参构造
    MyArray(int capacity):m_Capacity(capacity)
    {
        // cout << "MyArray的有参构造调用" << endl;
        this->m_Size = 0;
        this->m_Address = new T[this->m_Capacity];

    }
    //拷贝
    MyArray( const MyArray & arr)
    {
        cout << "MyArray的拷贝构造调用" << endl;
        this->m_Capacity = arr.m_Capacity;
        this->m_Size = arr.m_Size;
        this->m_Address = new T[arr.m_Capacity];
        for (int i = 0; i < this->m_Capacity; i++)
        {
            m_Address[i] = arr.m_Address[i];
        }
    }
    //析构
    ~MyArray()
    {
        // cout << "MyArray的析构构造调用" << endl;
        if (this->m_Address != nullptr)
        {
            delete[] m_Address;
            m_Address = nullptr;
        }
    }

    //深拷贝
    MyArray& operator = ( MyArray & arr)
    {
        //检查被赋值有没有被释放
        if (this->m_Address != nullptr)
        {
            delete []m_Address;
            m_Address = nullptr;
            this->m_Capacity = 0;
            this->m_Capacity = 0;
        }
        this->m_Address = new T[arr.m_Capacity];
        this->m_Capacity = arr.m_Capacity;
        this->m_Size  = arr.m_Size;
        return *this;
    }
    //重载中括号
    T& operator [](int index)
    {
        return this->m_Address[index];
    }
    //尾插法
    void Push_Back(int value)
    {
        if (this->m_Size < this->m_Capacity)
        {
            this->m_Address[this->m_Size] = value;
            this->m_Size++;
        }
        else
        {
            return;
        }
    }
    //尾删法
    void Pop_Back()
    {
        if (this->m_Size == 0)
        {
            return;
        }
        this->m_Size--;
    }
    //排序
    void MySort()
    {
        sort(this->m_Address, this->m_Address + this->m_Size);
    }
    //倒置
    void MyReverse()
    {
        reverse(this->m_Address, this->m_Address + this->m_Size);
    }

    //查找
    int MyFind(MyArray & arr, T target)
    {
        for (int i = 0; i < arr.m_Capacity; i++)
        {
            if (arr.m_Address[i] == target)
            {
                return i;
            }
        }
        return -1;
    }

    //求和
    T getSum()
    {
        T sum = 0;
        for (int i = 0; i < this->m_Capacity; i++)
        {
            sum += this->m_Address[i];
        }
        return sum;
    }

    void disPlay()
    {
        for (int i = 0; i < this->m_Capacity; i++)
        {
            cout << this->m_Address[i] << " ";
        }
        cout << endl;
    }
    int getCapacity()
    {
        return this->m_Capacity;
    }
    int getSize()
    {
        return this->m_Size;
    }
private:
    T * m_Address;

    int m_Capacity;//数组容量

    int m_Size;//数组大小
};
